/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.recorder;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import fr.uha.ensisa.jmp.drone.app.DroneConstants;
import fr.uha.ensisa.jmp.drone.recorder.FileWriter;


public class FileRecorder<T> extends Recorder<T>
{
	public static String DEFAULT_EXTENSION = " ";

	private String directory;
	private String extension;
	private String currentFileName;
	
	public FileRecorder(FileWriter<T> writer)
	{
		this(writer, System.getProperty("user.dir"),DEFAULT_EXTENSION);
	}

	public FileRecorder(FileWriter<T> writer, String directory, String extension)
	{
		super(writer);
		this.directory = directory;
		this.extension = extension;
		this.currentFileName ="";
	}
	
	public void setDirectory(String directory)
	{
		this.directory = directory;
	}

	public void setExtension(String extension)
	{
		this.extension = extension;
	}
	

	public void start() throws IOException
	{
		super.start();
		this.currentFileName = this.getFileName()+ ((this.extension != null)? "."+this.extension :"");
		((FileWriter<T>)this.writer).setPath(this.directory + this.currentFileName);
		this.writer.open();
	}
	
	public String getCurrentFileName()
	{
		return this.currentFileName;
	}
		
	protected String getFileName()
	{
		Calendar calendar = Calendar.getInstance();

		StringBuilder fname = new StringBuilder();
		fname.append(calendar.get(Calendar.YEAR)).append('_');
		fname.append(calendar.get(Calendar.DAY_OF_YEAR)).append('_');
		fname.append(calendar.get(Calendar.HOUR_OF_DAY)).append('_');
		fname.append(calendar.get(Calendar.MINUTE)).append('_');
		fname.append(calendar.get(Calendar.SECOND));
		return fname.toString();
	}
	
	public static void setupDirectories()
	{
		final String parentDir = System.getProperty("user.dir");
		
		new File(parentDir+DroneConstants.NAVDATA_DEMO_RECORDS_DIR).mkdirs();
		new File(parentDir+DroneConstants.VIDEO_RECORDS_DIR).mkdirs();
		new File(parentDir+DroneConstants.PICTURES_RECORDS_DIR).mkdirs();
	}}
